package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingPaperTypeEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.KDChangeAddressDAO;
import cj.web.admin.domain.KDBookingPaper;
import cj.web.admin.domain.KDChangeAddress;
import cj.web.login.controllers.BaseController;

@Path("/kdchangeaddress")
public class KDChangeAddressController extends BaseController{

	private static Logger logger = Logger.getLogger(KDChangeAddressController.class.getName());
	
	@Autowired
	private KDChangeAddressDAO kdChangeAddressDao;
	
	@Get("show")
	public String show(Model model,KDChangeAddress kdChangeAddress) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.kdgsgztb);
        
        EnumSet<KDBookingPaperTypeEnum> kdBookingPaperTypeEnum = EnumSet.allOf(KDBookingPaperTypeEnum.class);
        model.add("kdBookingPaperTypeEnum",kdBookingPaperTypeEnum);
        
		EnumSet<KDBookingStatusEnum> kdBookingStatusEnum = EnumSet.allOf(KDBookingStatusEnum.class);
        model.add("kdBookingStatusEnum",kdBookingStatusEnum);
        return "portal/kdChangeAddressList.vm";
	}

	@Post("getList")
	public String queryList(KDChangeAddress kdChangeAddress) {
	
		JSONObject jo = new JSONObject();
		
		List<KDChangeAddress> objs = kdChangeAddressDao.query(kdChangeAddress);
		
		for(KDChangeAddress obj:objs)
		{
			obj.setCreateAT(DateUtils.getMinusTime(obj.getCreateAT(), DateUtils.DATE_FULL_STR, 0));
			obj.setOldAddress(cityWuhan+KDAddressAreaEnum.getName(obj.getOldArea())+
					KDAddressStreetEnum.getName(obj.getOldStreet())+obj.getOldAddress());
			obj.setNewAddress(cityWuhan+KDAddressAreaEnum.getName(obj.getNewArea())+
					KDAddressStreetEnum.getName(obj.getNewStreet())+obj.getNewAddress());
		}
		
		int size = kdChangeAddressDao.querySzie(kdChangeAddress);
		
		jo.put("data", objs);
		jo.put("draw", kdChangeAddress.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.kdgsgztb);
        
        EnumSet<KDBookingPaperTypeEnum> kdBookingPaperTypeEnum = EnumSet.allOf(KDBookingPaperTypeEnum.class);
        model.add("kdBookingPaperTypeEnum",kdBookingPaperTypeEnum);
        
        KDChangeAddress obj = kdChangeAddressDao.queryById(id);
		model.add("obj", obj);
		
		return "portal/kdChangeAddressEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,KDChangeAddress kdChangeAddress,HttpServletRequest request)
	{
		kdChangeAddressDao.updateMemoById(kdChangeAddress.getId(), kdChangeAddress.getMemo());
		return "r:show";
	}
	
	@Get("updateStatus")
	public String updateStatus(Model model,@Param("id") int id)
	{
		kdChangeAddressDao.updateStatusById(id);
		return "r:show";
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		kdChangeAddressDao.deleteById(id);
		return "r:show";
	}
}
